"""
自定义样式的按钮组件
"""
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtCore import Qt, QSize
from PyQt5.QtGui import QIcon, QFont

from app.ui.theme import Theme


class StyledButton(QPushButton):
    """
    自定义样式的按钮组件
    """
    
    # 按钮类型
    PRIMARY = "primary"
    SECONDARY = "secondary"
    SUCCESS = "success"
    WARNING = "warning"
    ERROR = "error"
    INFO = "info"
    
    def __init__(self, text="", parent=None, button_type=PRIMARY, icon=None, icon_size=None):
        """
        初始化按钮
        
        @param text: 按钮文本
        @param parent: 父组件
        @param button_type: 按钮类型
        @param icon: 按钮图标
        @param icon_size: 图标大小
        """
        super().__init__(text, parent)
        self.button_type = button_type
        
        # 设置图标
        if icon:
            self.setIcon(QIcon(icon))
            if icon_size:
                self.setIconSize(QSize(icon_size, icon_size))
        
        # 应用样式
        self.update_style()
    
    def update_style(self):
        """
        更新按钮样式
        """
        # 获取主题颜色
        bg_color = Theme.get_color(self.button_type)
        text_color = "#FFFFFF"  # 按钮文字颜色通常为白色
        hover_color = self._adjust_color(bg_color, -15)  # 稍微暗一点的颜色用于悬停
        pressed_color = self._adjust_color(bg_color, -30)  # 更暗的颜色用于按下状态
        
        # 设置样式表
        self.setStyleSheet(f"""
            QPushButton {{
                background-color: {bg_color};
                color: {text_color};
                border: none;
                border-radius: 4px;
                padding: 8px 16px;
                font-weight: bold;
                font-size: 12px; /* 减小文字大小 */
            }}
            
            QPushButton:hover {{
                background-color: {hover_color};
            }}
            
            QPushButton:pressed {{
                background-color: {pressed_color};
            }}
            
            QPushButton:disabled {{
                background-color: {Theme.get_color("disabled")};
                color: {Theme.get_color("text_disabled")};
            }}
        """)
    
    def set_button_type(self, button_type):
        """
        设置按钮类型
        
        @param button_type: 按钮类型
        """
        self.button_type = button_type
        self.update_style()
    
    def _adjust_color(self, hex_color, amount):
        """
        调整颜色亮度
        
        @param hex_color: 十六进制颜色值
        @param amount: 调整量（正值变亮，负值变暗）
        @return: 调整后的颜色
        """
        hex_color = hex_color.lstrip('#')
        r, g, b = int(hex_color[0:2], 16), int(hex_color[2:4], 16), int(hex_color[4:6], 16)
        
        r = max(0, min(255, r + amount))
        g = max(0, min(255, g + amount))
        b = max(0, min(255, b + amount))
        
        return f"#{r:02x}{g:02x}{b:02x}"
    
    @staticmethod
    def set_primary_style(button):
        """
        将普通QPushButton设置为primary样式
        
        @param button: QPushButton对象
        """
        button.setProperty("primary", "true")
        button.style().unpolish(button)
        button.style().polish(button) 